package com.lovo.dao;

import org.springframework.beans.factory.annotation.Value;

import java.sql.*;

public class BaseDAO {
    @Value("${dbclass}")
    private String dbclass;
    @Value("${dburl}")
    private String dburl;
    @Value("${dbuser}")
    private String dbuser;
    @Value("${dbpass}")
    private String dbpass;
    public Connection getConnection(){
        Connection con = null;
        try {
            Class.forName(dbclass);
            con = DriverManager.getConnection(dburl,dbuser,dbpass);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return con;
    }
    public void close(Connection con, Statement statement){
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public void close(Connection con, Statement statement, ResultSet rs){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        close(con,statement);
    }
}
